package com.supply.dl.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.supply.dl.R;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.RecyclerViewDivider;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.TouliaoOrderInfo;
import com.supply.dl.bean.TouliaoPutOrderInfo;
import com.supply.dl.utils.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 投料单列表(投料二级列表)
 * <p>
 * Author: tian <br>
 * Time: 2018/8/15 16:10
 */
public class FeedSheetListActivity extends BaseActivity {

    @BindView(R.id.search_rv)
    RecyclerView search_rv;

    /**
     * 输入框
     */
    @BindView(R.id.input_et)
    EditText input_et;

    private NormalAdapter<TouliaoPutOrderInfo> adapter;

    private List<TouliaoPutOrderInfo> datas;

    /**
     * 订单id
     */
    private String orderId;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_feed_sheetlist;
    }

    @Override
    protected void initView() {
        setTitleShow("投料单列表");
        showLeftBack();
        setRightInVisible();

        orderId = getIntent().hasExtra("orderId") ? getIntent().getStringExtra("orderId") : "";

        datas = new ArrayList<>();
        adapter = new NormalAdapter<TouliaoPutOrderInfo>(this, datas) {
            @Override
            protected void convert(ViewHolder holder, TouliaoPutOrderInfo data) {
                holder.setText(R.id.name_tv, "投料单:" + data.getNoticeOrderNo());
                holder.setText(R.id.status_tv, data.getOrderStatus());
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_feed_sheet;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<TouliaoPutOrderInfo>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, TouliaoPutOrderInfo data, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("noticeOrderId", data.getId());
                bundle.putSerializable("noticeOrderNo", data.getNoticeOrderNo());
                jumpActivity(FeedOperationDetailActivity.class, bundle);
            }
        });

        search_rv.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        search_rv.setLayoutManager(new LinearLayoutManager(this));
        search_rv.setAdapter(adapter);

        getTouliaoPutOrders();
    }


    /**
     * 根据订单id获取 投料单列表
     */
    private void getTouliaoPutOrders() {

        String keyWord = StringUtils.isCanUse(input_et.getText().toString()) ?
                input_et.getText().toString() : "";

        ApiService api = HttpMethod.getInstance().create(ApiService.class);
        api.getTouliaoPutOrderList(orderId, keyWord)
                .compose(RxSchedulers.<BaseInfo<List<TouliaoPutOrderInfo>>>compose(this))
                .subscribe(new BaseObserver<List<TouliaoPutOrderInfo>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<TouliaoPutOrderInfo>> baseInfo) {

                        if (baseInfo != null && baseInfo.getData() != null) {
                            datas.clear();
                            datas.addAll(baseInfo.getData());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }


    @OnClick(R.id.create_feed_sheet_btn)
    public void onClick(View v) {

        if (v.getId() == R.id.create_feed_sheet_btn) {
            Bundle bundle = new Bundle();
            bundle.putString("orderId",orderId);
            bundle.putString("omsOrderNo",getIntent().getStringExtra("omsOrderNo"));
            jumpActivity(CreateFeedSheetActivity.class,bundle);
        }
    }

}










